Skip to content

Comments

[BE] 기능 개발 테스트 코드 작성하기#303

Merged
jihaneol merged 3 commits intodevelopfrom
be/feat/293
Apr 15, 2025
Merged

[BE] 기능 개발 테스트 코드 작성하기#303
jihaneol merged 3 commits intodevelopfrom
be/feat/293

Conversation

@jihaneol
Copy link
Contributor

@jihaneol jihaneol commented Apr 11, 2025


🚀 어떤 기능을 구현했나요 ?

  • 리뷰 테스트 코드 작성

🔥 어떤 문제를 마주했나요 ?

  1. 다중 dataSource로 test yml 설정에서 mode=mysql로 지정안해서 안됐던 문제
  2. 레디스 사용으로 config 파일을 test 용으로 새로 작성 mock으로 감싸주기.
  3. UserResolver를 사용할때 session이 redis와 같이 사용해서 session 주입이 아닌 Resolver를 mock으로 만들어서 직접 주입
  4. MockBean -> MockitoBean으로 변경.
  5. BeforeAll 사용시 static이 사용되는데 이때 @testinstance(TestInstance.Lifecycle.PER_CLASS) 사용 하면 static으로 안해도 된다.
    @testinstance(TestInstance.Lifecycle.PER_CLASS) 의미
    테스트 클래스 인스턴스를 한 번만 생성하고 재사용합니다.

✨ 어떻게 해결했나요 ?

Spring Session + Redis 구성은:
MockMvc.perform() → 내부적으로 SessionRepositoryFilter 가 동작

세션 객체를 Redis 기반으로 wrapping 해버림

네가 넣은 MockHttpSession이 아닌 Spring Session 관리 세션 사용됨

그래서 세션 값이 안들어가져서 Resolver를 mock으로 만들어 주입.

     given(currentUserResolver.supportsParameter(any()))
                .willReturn(true);
        given(currentUserResolver.resolveArgument(any(), any(), any(), any()))
                .willReturn(user);// 강제 리턴

📝 어떤 부분에 집중해서 리뷰해야 할까요?

📚 참고 자료 및 회고

- 인메모리 설정
- 테스트값 생성

🔗 Resolves: #293
- 레디스 config mock 대체
- resolve mock 대체
- mockBean -> mockitoBean

🔗 Resolves: #293
@jihaneol jihaneol changed the title Be/feat/293 [BE] 기능 개발 테스트 코드 작성하기 Apr 12, 2025
@kiteof-park
Copy link
Contributor

PR 이전보다 구체적으로 작성해서 좋습니다 ~! 👍👍

import org.springframework.context.annotation.Profile;

@Configuration
@Profile("!test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RedissonConfig에 @Profile("!test")를 사용한 이유가
테스트 코드 작성시에 RedissonConfig를 Mock으로 사용하기 위함인가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 이걸 사용하지 않으면 springboottest 시작시 테스트로 만든 것이 기존의 confiquration과 겹치거나 무시되서 사용했습니다.

Comment on lines +113 to +125
User user = User.builder()
.loginId("testuser@example.com") // 로그인 아이디 (이메일 또는 소셜 ID)
.password("testPassword123!") // 비밀번호 (소셜 로그인은 null 가능)
.name("홍길동") // 이름
.birthDate("1995-05-01") // 생년월일
.gender("M") // 성별 (M/F)
.phone("010-1234-5678") // 휴대폰 번호
.provider(LoginType.LOCAL) // 로그인 타입 (enum 값)
.ageAgreement(true) // 만 14세 이상 동의
.useAgreement(true) // 서비스 이용 약관 동의
.picAgreement(true) // 개인정보 처리 방침 동의
.adAgreement(false) // 광고 수신 동의
.build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 코드에서는 fixture를 사용하는 방법도 있다고 알고있는데, 어떻게 생각하시나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixture 사용은 추후에 사용할 예정입니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 다른 곳에서 재사용하지 않아서 필요시 만들 예정입니다.

@soyun-i
Copy link
Contributor

soyun-i commented Apr 14, 2025

회원 테스트 코드를 어떻게 짤 수 있나 고민했는데 좋은 참고자료 감사합니다! 😊

Long result = 리뷰작성(request);

// then
Assertions.assertNotNull(result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 리뷰가 null인지만 체크하면 좀 부족하지 않을까요 내용이 예상대로 들어갔는지 같은 좀 더 구체적인 검증이 추가되면 어떨까요?

Copy link
Contributor Author

@jihaneol jihaneol Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋습니다! 감사합니다!

@jihaneol jihaneol merged commit 6688b32 into develop Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants